package com.cgw.room.util;

import android.os.Build;
import android.util.Log;

import com.cgw.room.tools.ByteOrderOperator;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileOperator {
    public static boolean saveFloatArrayAsShortArray2Bin(String filename, float[] data) {

        Log.i("[UPLOAD]", " data Len = " + data.length);

        DataOutputStream dos = null;
        try {
            Log.i("[filename]", filename);
            File file = new File(filename);
            FileOutputStream fos = new FileOutputStream(file);
            dos = new DataOutputStream(fos);

            int n = 0;
            for (float f : data) {
                ++n;
                //dos.writeFloat(f);
                dos.writeShort((short)f);
            }


            Log.i("[UPLOAD]", " Write Len = " + n);

            // force bytes to the underlying stream
            dos.flush();
            fos.flush();

            dos.close();
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static float[] readBinAsShort2FloatArray(String filename, boolean bBigEndFile) {
        try {
            Log.i("[filename]", filename);
            File file = new File(filename);
            FileInputStream fin = new FileInputStream(file);

            int len = fin.available() / 2;
            short[] shorts = new short[len];
            float[] floats = new float[len];
            byte[] bytes = new byte[fin.available()];
            fin.read(bytes);
            fin.close();

            if (bBigEndFile){
                ByteOrderOperator.changeByteOrder(bytes, 2);
            }

            BufferUtil.byteBuffer2ShortBuffer(bytes, shorts);
            for (int n = 0 ; n < len; ++n){
                floats[n] = shorts[n];
            }
            return floats;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static short[] readBinAsShortArray(String filename, boolean bBigEndFile) {
        try {
            Log.i("[TS]", filename);
            File file = new File(filename);
            FileInputStream fin = new FileInputStream(file);

            int len = fin.available() / 2;
            short[] shorts = new short[len];
            byte[] bytes = new byte[fin.available()];
            fin.read(bytes);
            fin.close();

            if (bBigEndFile){
                ByteOrderOperator.changeByteOrder(bytes, 2);
            }

            BufferUtil.byteBuffer2ShortBuffer(bytes, shorts);
            return shorts;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean saveEmptyFile(String filename) {
        try {
            File file = new File(filename);
            FileOutputStream fos = new FileOutputStream(file);
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean writeString(String filename, String s) {
        try {
            File file = new File(filename);
            if (file.exists())
                file.delete();

            FileOutputStream fos = new FileOutputStream(file);
            fos.write(s.getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String readString(String filename) {
        String s = "";
        try {
            File file = new File(filename);
            FileInputStream fin = new FileInputStream(file);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                s = new String(fin.readAllBytes());
            }
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

    public static boolean delFile(String filename){
        File file = new File(filename);
        return file.delete();
    }

    public static boolean delFileInDir(String dirname){
        File rootDir = new File(dirname);
        File[] listDirs = rootDir.listFiles();
        if (listDirs == null || listDirs.length == 0)
            return true;

        boolean bSuccess = true;
        for (File dir : listDirs) {
            if (!dir.isDirectory())
                continue;

            File[] listFiles = dir.listFiles();
            if (listFiles == null || listFiles.length == 0)
                continue;

            boolean bSubSuccess = true;
            for (File file : listFiles) {
                if (!file.isFile())
                    continue;

                bSubSuccess &= file.delete();
            }

            if (bSubSuccess){
                bSuccess &= dir.delete();
            }

            bSuccess &= bSubSuccess;
        }

        return bSuccess;
    }
}
